﻿using System;
using Irony.Ast;
using Irony.Parsing;
using LibraryGuard.Interfaces;

namespace LibraryGuard.Internals.AST
{
	/// <summary>
	/// Represents variables and identifiers.
	/// </summary>
	internal class Identifier
		: IValue
	{
		#region Fields
		public readonly string Name = string.Empty;
		#endregion

		#region Constructors
		public Identifier(string name)
		{
			this.Name = name;
		}
		#endregion

		#region IValue Members
		/// <inheritdoc />
		public TypeCode GetBaseType(IDataProvider data)
		{
			return data.GetVariableType(this.Name);
		}

		/// <inheritdoc />
		public bool CanBeConvertedTo(TypeCode type, IDataProvider data)
		{
			return data.GetVariable(this.Name).CanBeConvertedTo(type, data);
		}

		/// <inheritdoc />
		public object GetValue(TypeCode type, IDataProvider data)
		{
			return data.GetVariable(this.Name).GetValue(type, data);
		}
		#endregion

		#region Statics
		public static void Create(AstContext context, ParseTreeNode parseNode)
		{
			parseNode.AstNode = new Identifier(parseNode.Token.Text);
		}
		#endregion
	}
}
